#!/bin/sh

ram() {

PAGE_SIZE=$(getconf PAGE_SIZE)

for pid in /proc/*/
do
  if [ -a "$pid/statm" ]
  then
    MEM=`cat "$pid/statm" | awk "{print \\$2*\$PAGE_SIZE}"`
    if [ $MEM != 0 ]
    then
      CMD=`cat $pid/cmdline`
      echo "$MEM" "$CMD"
    fi
  fi
done

}

ram | sort -n | awk '{ sum=$1 ; hum[1024**3]="Gb";hum[1024**2]="Mb";hum[1024]="Kb"; for (x=1024**3; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f%s %s\n",sum/x,hum[x],$2;break } }}'

# Unit conversion trick taken from: http://ubuntuforums.org/showthread.php?t=1795681
